/********************************************
 *      系统时钟配置
 * Created by zlj on 2025/4/3.
********************************************/
#include "app_clock.h"

/**
 * 系统默认主频时钟设置：8M 16M 24M
 * @param sysclk
 */
void SystemClockSel_RCHF(SysClk sysclk){
    FL_RCC_RCHF_Enable();
    switch(sysclk)
    {
        case SYSCLK_RCHF_8M:
            FL_RCC_RCHF_SetFrequency(FL_RCC_RCHF_FREQUENCY_8MHZ);
            FL_RCC_RCHF_WriteTrimValue(RCHF8M_TRIM);
            FL_FLASH_SetReadWait(FLASH, FL_FLASH_READ_WAIT_0CYCLE);      // <24M,0 WAIT CYCLE
            break;

        case SYSCLK_RCHF_16M:
            FL_RCC_RCHF_SetFrequency(FL_RCC_RCHF_FREQUENCY_16MHZ);
            FL_RCC_RCHF_WriteTrimValue(RCHF16M_TRIM);
            FL_FLASH_SetReadWait(FLASH, FL_FLASH_READ_WAIT_0CYCLE);      // <24M,0 WAIT CYCLE
            break;

        case SYSCLK_RCHF_24M:
            FL_RCC_RCHF_SetFrequency(FL_RCC_RCHF_FREQUENCY_24MHZ);
            FL_RCC_RCHF_WriteTrimValue(RCHF24M_TRIM);
            FL_FLASH_SetReadWait(FLASH, FL_FLASH_READ_WAIT_0CYCLE);      // <24M,0 WAIT CYCLE
            break;

        default:
            FL_RCC_RCHF_SetFrequency(FL_RCC_RCHF_FREQUENCY_8MHZ);
            FL_RCC_RCHF_WriteTrimValue(RCHF8M_TRIM);
            FL_FLASH_SetReadWait(FLASH, FL_FLASH_READ_WAIT_0CYCLE);      // <24M,0 WAIT CYCLE
            break;
    }

    FL_RCC_SetSystemClockSource(FL_RCC_SYSTEM_CLK_SOURCE_RCHF);
    FL_RCC_SetAHBPrescaler(FL_RCC_AHBCLK_PSC_DIV1);
    FL_RCC_SetAPB1Prescaler(FL_RCC_APB1CLK_PSC_DIV1);
    FL_RCC_SetAPB2Prescaler(FL_RCC_APB2CLK_PSC_DIV1);
    SystemCoreClockUpdate();
}

uint32_t SystemClockGet(SysClk systemClock)
{
    switch(systemClock)
    {
        case SYSCLK_RCHF_8M:
            return 8000000;
        case SYSCLK_RCHF_16M:
            return 16000000;
        case SYSCLK_RCHF_24M:
            return 24000000;
    }

    return 0;
}